﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Telerik.Web.Mvc.UI;
using TelerikMvcClientApi.Models;

namespace TelerikMvcClientApiRazor.Controllers
{
    public class TreeViewController : BaseController
    {
        public ActionResult ShowContextMenu()
        {
            return View();
        }

        public ActionResult ClientSideTreeNode()
        {
            return View();
        }

        public ActionResult ClientSideAddRemoveNodes()
        {
            return View();
        }

        public JsonResult SelectTreeItems()
        {
            List<TreeViewItem> treeNodeList = new List<TreeViewItem>();

            TreeViewItem tviRoot = new TreeViewItem
            {
                Text = "Telerik Products",
                Value = "Telerik Products",
                Expanded = true
            };
            treeNodeList.Add(tviRoot);

            IList<Product> productList = this.GetProducts();

            var categories = (from Product p in productList
                              select p.Category).Distinct();

            foreach (string category in categories)
            {
                TreeViewItem tviCategory = new TreeViewItem
                {
                    Text = category,
                    Value = category,
                    Expanded = true
                };
                tviRoot.Items.Add(tviCategory);

                var subCategories = (from Product p in productList
                                     where p.Category == category
                                     select p.SubCategory).Distinct();

                foreach (string subCategory in subCategories)
                {
                    TreeViewItem tviSubCategory = null;

                    if (subCategory.Length > 0)
                    {
                        tviSubCategory = new TreeViewItem
                         {
                             Text = subCategory,
                             Value = subCategory,
                             Expanded = true
                         };
                        tviCategory.Items.Add(tviSubCategory);
                    }

                    var products = (from Product p in productList
                                    where p.Category == category &&
                                          p.SubCategory == subCategory
                                    select p);

                    foreach (Product product in products)
                    {
                        TreeViewItem tviProduct = new TreeViewItem
                        {
                            Text = product.Name,
                            Value = product.Id.ToString(),
                            Expanded = true
                        };
                        if (tviSubCategory != null)
                        {
                            tviSubCategory.Items.Add(tviProduct);
                        }
                        else
                        {
                            tviCategory.Items.Add(tviProduct);
                        }
                    }
                }
            }

            return Json(treeNodeList, JsonRequestBehavior.AllowGet);
        }
    }
}
